package mail;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

public class mailUtil {
    final String USERNAME = "zmczmckkk@outlook.com";
    final String getPASSWORD = "Beauty6499!@#";
    public static void receivepop3Email(String USER_NAME, String PASSWORD) {
        String host = "outlook.office365.com";// change accordingly
        String mailStoreType = "pop3";
        String popPort = "995";
        Store store = null;
        // 配置连接信息
        Properties properties = new Properties();
        properties.put("mail.store.protocol", mailStoreType);
        properties.put("mail.pop3.host", host);
        properties.put("mail.pop3.port", popPort);
        properties.put("mail.pop3.starttls.enable", "true");
        try {
            Session emailSession = Session.getDefaultInstance(properties);
            //打开调式模式，可看到邮件发送过程中各步骤的详细信息
//            emailSession.setDebug(true);
            store = emailSession.getStore("pop3s");
            store.connect(host, USER_NAME, PASSWORD);
            Folder emailFolder = store.getFolder("INBOX");
            emailFolder.open(Folder.READ_ONLY);
            Message[] messages = emailFolder.getMessages();
            for (Message msg:messages) {
                System.out.println(msg.getSubject());
            }
            emailFolder.close(true);
            store.close();
        } catch (NoSuchProviderException e) {
            e.printStackTrace();
        } catch (MessagingException e) {
            e.printStackTrace();
        }finally {
            if(null != store){
                try {
                    store.close();
                } catch (MessagingException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    String  USER_NAME   ="*****@outlook.com";
    String  PASSWORD    = "******";
    public static void receiveimapEmail(String USER_NAME, String PASSWORD) {
        String host = "outlook.office365.com";// change accordingly
        String mailStoreType = "imap";
        String popPort = "993";
        try {
            // 配置连接信息
            Properties properties = new Properties();
            properties.put("mail.store.protocol", mailStoreType);
            properties.put("mail.imap.host", host);
            properties.put("mail.imap.port", popPort);
            properties.put("mail.imap.starttls.enable", "true");
            Session emailSession = Session.getDefaultInstance(properties);
//            emailSession.setDebug(true);
            Store store = emailSession.getStore("imaps");
            store.connect(host, USER_NAME, PASSWORD);
            Folder emailFolder = store.getFolder("INBOX");
            emailFolder.open(Folder.READ_ONLY);
            Message[] messages = emailFolder.getMessages();
            for (Message msg:messages) {
                System.out.println(msg.getSubject());
            }
            emailFolder.close(true);
            store.close();
        } catch (NoSuchProviderException e) {
            e.printStackTrace();
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }


    public static boolean sendSMTPMail(String USER_NAME, String PASSWORD, String to, String text, String title) {
        String host = "smtp.office365.com";
        String mailStoreType = "smtp";
        String popPort = "587";
        final Properties props = new Properties();

        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.host", host);
        props.put("mail.store.protocol", mailStoreType);
        //x修复邮箱
        props.put("mail.smtp.ssl.protocols", "TLSv1.2");

        props.put( "mail.smtp.port", popPort );
        //开启SSL
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.socketFactory.port",popPort);
        props.put("mail.smtp.socketFactory.fallback","false");
        try {
            Session session = Session.getDefaultInstance(props, new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(USER_NAME, PASSWORD);//账号密码
                }
            });
            session.setDebug(true);
            // 创建邮件消息
            MimeMessage message = new MimeMessage(session);
            // 设置发件人
            InternetAddress form = new InternetAddress(USER_NAME);
            message.setFrom(form);
            // 设置收件人
            InternetAddress toAddress = new InternetAddress(to);
            message.setRecipient(Message.RecipientType.TO, toAddress);
            // 设置邮件标题
            message.setSubject(title);
            // 设置邮件的内容体
            message.setContent(text, "text/html;charset=UTF-8");
            // 发送邮件
            Transport.send(message);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public static void main(String[] args) {
        sendSMTPMail("zmczmckkk@outlook.com","Beauty6499!@#","zmczmckkk@outlook.com"
        ,"HELLO", "MOTO");

    }
}
